package com.buding.fx.utils;

import com.buding.fx.common.enums.AlertDialogTypeEnum;
import com.buding.fx.common.enums.SearchNodeType;
import com.buding.fx.common.enums.SizeEnum;
import com.buding.fx.entity.FormSearchParams;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Orientation;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.DragEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.shape.StrokeType;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;


/**
 * @author mingdongwei-jwk
 * @date 2022/10/14 10:37
 * @class com.buding.fx.utils
 * @Description 自定义对话框
 */
public class AlertUtils extends Application {
    private Stage stage;
    @Override
    public void start(Stage primaryStage) throws Exception {
        Stage stage = new Stage(StageStyle.TRANSPARENT);
        this.stage = stage;
        VBox dialog = (VBox) buildDialog("标签组","这不是一个正常操作，可能会出现未知的错误，请谨慎操作!",SizeEnum.L5,AlertDialogTypeEnum.INFOR);
        Scene scene = new Scene(dialog);
        String css = "css/buding-fx-css-main.css";
        scene.getStylesheets().add(css);
        scene.setFill(Color.web("#ffffff00"));
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }


    private Node buildDialog(String headTitle,String contentStr,SizeEnum sizeEnum,AlertDialogTypeEnum alertDialogTypeEnum){
        VBox rootBox = new VBox();
        rootBox.getStyleClass().setAll("bd-alt");
        double width  =  sizeEnum.getWidth();
        double height = width*0.5;
        double fontSize = sizeEnum.getFontSize();
        double imgSize = sizeEnum.getIconSize();
        AlertDialogTypeEnum dialogTypeEnum = AlertDialogTypeEnum.DEFULT;
        rootBox.setPrefWidth(width);
        rootBox.setMaxHeight(height);
        rootBox.setAlignment(Pos.TOP_CENTER);
        Node title   = buideTitleNode(width-10 , 25, headTitle,alertDialogTypeEnum.getIcon());
        title.getStyleClass().setAll("bd-alt");
        //Node content = buildDialogContent(width-10,height-25,contentStr,alertDialogTypeEnum);
        List<FormSearchParams> params = new ArrayList<>();
        FormSearchParams formSearchParams = new FormSearchParams();
        formSearchParams.setFiledName("用户名：");
        formSearchParams.setSearchNodeType(SearchNodeType.TEXT);
        params.add(formSearchParams);
        FormSearchParams formSearchParams2 = new FormSearchParams();
        formSearchParams2.setFiledName("邮箱");
        formSearchParams2.setSearchNodeType(SearchNodeType.TEXT);
        params.add(formSearchParams2);
        FormSearchParams formSearchParams3 = new FormSearchParams();
        formSearchParams3.setFiledName("手机");
        formSearchParams3.setSearchNodeType(SearchNodeType.TEXT);
        params.add(formSearchParams3);
        Node content = buildFormContent(width-10,height-25,params,alertDialogTypeEnum);
        rootBox.getChildren().addAll(title,content);
        VBox.setMargin(title,new Insets(0,5,0,5));
        VBox.setMargin(content,new Insets(0,0,5,0));
        return rootBox;
    }


    /**
     * 弹出对话框
     * @param headTitle
     * @param sizeEnum
     * @param params
     * @return
     */
    private Node buildFormInput(String headTitle, SizeEnum sizeEnum, List<FormSearchParams> params){
        VBox rootBox = new VBox();
        Node title   = buideTitleNode(sizeEnum.getWidth()-10 , 25, headTitle,AlertDialogTypeEnum.PRIMARY.getIcon());
        title.getStyleClass().setAll("bd-alt");
        double width  =  sizeEnum.getWidth();
        double height = width*0.5;
        Node content = buildFormContent(width-10,height-25,params,AlertDialogTypeEnum.INFOR);
        rootBox.getChildren().addAll(title,content);
        return rootBox;
    }


    private Node buideTitleNode(double wigth, double height,String title,String titleIcon){
        //采用左右两边布局的效果，左边lable，右边关闭框
        BorderPane borderPane = new BorderPane();
        borderPane.setPrefWidth(wigth);
        borderPane.setMaxHeight(height);
       // gridPane.setStyle("-fx-background-color: #928d8d");
        double childrenWidth = wigth/2;
        double childrenHeight = height;
        HBox hbox = new HBox();
        hbox.setPrefWidth(childrenWidth);
        hbox.setPrefHeight(childrenHeight);
        hbox.setAlignment(Pos.CENTER_LEFT);
       // hbox.setStyle("-fx-background-color: yellow");
        Label label = new Label(title);
        Image image = new Image(titleIcon);
        ImageView imageView = new ImageView(image);
        imageView.setFitWidth(15);
        imageView.setFitHeight(15);
        label.setGraphic(imageView);
        hbox.getChildren().add(label);

        HBox closeBox = new HBox();
        closeBox.setPrefWidth(childrenWidth);
        closeBox.setPrefHeight(childrenHeight);
        closeBox.setAlignment(Pos.TOP_RIGHT);
      //  closeBox.setStyle("-fx-background-color: gray");

        //45*18
        HBox closeHbox = new HBox();
        closeHbox.setPrefWidth(40);
        closeHbox.setMaxHeight(18);
        closeHbox.getStyleClass().setAll("close-lable");
        Border border = new Border(new BorderStroke(Color.web("#A19F9F"), BorderStrokeStyle.SOLID,new CornerRadii(3),new BorderWidths(0,1,1,1)));
        closeHbox.setBorder(border);
        closeHbox.setAlignment(Pos.CENTER);
        ImageView closeImg = new ImageView(new Image("img/icon/close-small.png"));
        closeImg.setFitWidth(17);
        closeImg.setFitHeight(17);
        closeHbox.getChildren().add(closeImg);
        closeBox.getChildren().add(closeHbox);
        closeHbox.addEventHandler(MouseEvent.MOUSE_PRESSED, new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                if (stage !=null){
                    stage.close();
                }
            }
        });
        borderPane.setLeft(hbox);
        borderPane.setRight(closeBox);
        return borderPane;

    }

    /**
     * 构建弹出对话框的主要内容部分
     * @param width
     * @param height
     * @param content
     * @param alertType
     * @return
     */
    private Node buildDialogContent(double width,double height,String content,AlertDialogTypeEnum alertType){
        BorderPane borderPane = new BorderPane();
        borderPane.setMaxWidth(width);
        borderPane.setMaxHeight(height-10);
        borderPane.getStyleClass().setAll("bd-alt-content");

        double buttonHeight = 40;
        GridPane gridPane = new GridPane();
        gridPane.setMaxHeight(height-buttonHeight);
        gridPane.setPadding(new Insets(10,10,10,10));
        gridPane.setHgap(10);
        Image image  = new Image(alertType.getIcon());
        ImageView imageView = new ImageView(image);
        imageView.setFitHeight(30);
        imageView.setFitWidth(30);


        Label cLabel = new Label();
        cLabel.setText(content);
        cLabel.getStyleClass().setAll("bd-alt-textarea");


        gridPane.getChildren().add(imageView);
        GridPane.setMargin(imageView,new Insets(0,0,0,0));
        gridPane.add(cLabel,1,0);

        HBox buttonsPane = new HBox();
        buttonsPane.setPrefWidth(width);
        buttonsPane.setPrefHeight(buttonHeight);
        buttonsPane.setStyle("-fx-background-color: #F0F0F0");
        buttonsPane.setAlignment(Pos.CENTER_RIGHT);
        buttonsPane.setPadding(new Insets(0,10,0,0));
        buttonsPane.setSpacing(20);
       // buttonsPane.setOrientation(Orientation.HORIZONTAL);  //水平
        Button cancle = new Button("取消(N)");
        Button confirm = new Button("确认(Y)");
        cancle.addEventHandler(MouseEvent.MOUSE_PRESSED, new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                System.out.println("cancle");
                if (stage !=null){
                    stage.close();
                }
            }
        });
        buttonsPane.getChildren().addAll(confirm,cancle);

        borderPane.setTop(gridPane);
        borderPane.setCenter(buttonsPane);

        return borderPane;
    }

    /**
     * 构建弹出表单主体内容
     * @param width
     * @param height
     * @param params
     * @param alertType
     * @return
     */
    private Node buildFormContent(double width, double height, List<FormSearchParams> params, AlertDialogTypeEnum alertType){
        BorderPane borderPane = new BorderPane();
        borderPane.setMaxWidth(width);
        borderPane.setMaxHeight(height-10);
        borderPane.getStyleClass().setAll("bd-alt-content");

        double buttonHeight = 40;
        GridPane gridPane = new GridPane();
        gridPane.setMaxHeight(height-buttonHeight);
        //gridPane.setPadding(new Insets(10,10,10,10));
        //gridPane.setHgap(10);
        gridPane.setAlignment(Pos.CENTER);

        for (int i = 0; i < params.size(); i++) {
            SearchNodeType type = params.get(i).getSearchNodeType();
            if (type == SearchNodeType.TEXT){
                //如果是文本类型
                Label label = new Label(params.get(i).getFiledName());
                TextField textField = new TextField();
                gridPane.add(label,0,i);
                gridPane.add(textField,1,i);
                GridPane.setMargin(label,new Insets(5,0,0,0));
                GridPane.setMargin(textField,new Insets(5,0,0,0));
            }
        }

        HBox buttonsPane = new HBox();
        buttonsPane.setPrefWidth(width);
        buttonsPane.setPrefHeight(buttonHeight);
        buttonsPane.setStyle("-fx-background-color: #F0F0F0");
        buttonsPane.setAlignment(Pos.CENTER_RIGHT);
        buttonsPane.setPadding(new Insets(0,10,0,0));
        buttonsPane.setSpacing(20);
        // buttonsPane.setOrientation(Orientation.HORIZONTAL);  //水平
        Button cancle = new Button("取消(N)");
        Button confirm = new Button("确认(Y)");
        cancle.addEventHandler(MouseEvent.MOUSE_PRESSED, new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                System.out.println("cancle");
                if (stage !=null){
                    stage.close();
                }
            }
        });
        buttonsPane.getChildren().addAll(confirm,cancle);

        borderPane.setTop(gridPane);
        borderPane.setCenter(buttonsPane);

        return borderPane;
    }

    private String adjContent(double width,String content){
        double strWidth = content.length()*2;
        if (strWidth<width){
            return  content;
        }
        BigDecimal bigDecimal = new BigDecimal(strWidth);
        BigDecimal bigDecimal1 = new BigDecimal(width);
        int i = bigDecimal.multiply(bigDecimal1).intValue();
        return insertString(content,"\n",i);
    }

    public static String insertString(String ors,String ins,int count) {
        StringBuilder sb = new StringBuilder(ors);	//将String转为StringBuilder
        int times = (ors.length()+count-1)/count - 1;			//计算出有多少个位置需要插入
        for(int i=times;i >=1 ;i--) {
            sb.insert(i*count, ins);				//需要从字符串的末尾开始插入，如果从前面插入，还需要计算后面的位置
        }
        return sb.toString();
    }

}
